/*
 * @Author: hanyuanjiao
 * @Date: 2025-07-21 07:18:54
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-07-24 04:38:56
 * @Description: 工具类与基本测试单元写在这里
 */
#include "instr.h"
#include "simu8051.h"
#include <stdio.h>
#include "tools.h"
#include "mem.h"

void tool_test0()
{
    char *result = u8toBinary(126);
    printf("%s\n", result);
}

void test_memory(void)
{
    static uint8_t data[MEM_XRAM_SIZE];
    //可位寻址区域
    for (int i = 0; i < 128; i++)
    {
        simu8051_write(MEM_TYPE_BIT, i, 1);
        data[i] = simu8051_read(MEM_TYPE_BIT, i);
    }
    for (int i = 128; i < 256; i++)
    {
        simu8051_write(MEM_TYPE_BIT, i, 1);
        data[i] = simu8051_read(MEM_TYPE_BIT, i);
    }
    // iram
    for (int i = 0; i < MEM_IRAM_SIZE; i++)
    {
        simu8051_write(MEM_TYPE_IRAM, i, i);
    }
    for (int i = 0; i < MEM_IRAM_SIZE; i++)
    {
        data[i] = simu8051_read(MEM_TYPE_IRAM, i);
    }
    // xram
    for (int i = 0; i < MEM_XRAM_SIZE; i++)
    {
        simu8051_write(MEM_TYPE_XRAM, i, i);
        data[i] = simu8051_read(MEM_TYPE_XRAM, i);
    }
    // sfr
    // only test acc, addr=0xe0
    simu8051_write(MEM_TYPE_SFR, SFR_ACC, 0x12);
    data[0] = simu8051_read(MEM_TYPE_SFR, SFR_ACC);
    if (data[0] != 0x12)
    {
        printf("sfr memory test failed\n");
    }
}